package com.ceba.test.modules.admin.dao;


import abc.data.dataSource.SqlDataSource;
import com.ceba.base.web.dao.IBaseDao;
import com.ceba.test.modules.admin.entity.TemplateWord;

import abc.data.DataRow;
import abc.data.DataTable;
import abc.data.Sql;
import abc.data.SqlSessionManager;
import abc.data.store.IStore;
import abc.data.store.StoreManager;
import abc.object.XObject;

public class TemplateWordDao extends IBaseDao {

	
	
	/**
	 * 根据id获取
	 * @param Id
	 * @return
	 * @throws Exception
	 */
	public  DataTable getDataTableById(String Id) throws Exception{
		Sql sql  = new Sql("SELECT ID,WORD_NAME,WORD_PATH,WORD_GENERATE_NAME,WORD_VIRTUAL_PATH FROM templates_word where ID='"+Id+"'");
		
		return SqlSessionManager.getDataTable(sql);

	}
	
	/**
	 * 获取所有
	 * @return
	 * @throws Exception
	 */
	public  DataTable getDataTable() throws Exception{
		Sql sql  = new Sql("SELECT ID,WORD_NAME,WORD_PATH,"
				+ " WORD_GENERATE_NAME,WORD_VIRTUAL_PATH  FROM templates_word ");
		
		return SqlSessionManager.getDataTable(sql);

	}

	/**
	 * 获取授权的封面尾页
	 * @return
	 * @throws Exception
	 */
	public  DataTable queryMergeAu(String userId) throws Exception{
		Sql sql = new Sql(
				"select g_report_merge.ID,NAME  from g_report_merge where g_report_merge.ID in (select g_user_cor_stamp.USER_AUTHORIZATION_ID  from "+
						" g_user_cor_stamp where g_user_cor_stamp.USER_ID='"+userId+"')");
		return SqlSessionManager.getDataTable(sql);

	}

	/**
	 * 新增
	 * @return
	 */
	public boolean add(IStore store,XObject object){
	
		if(store.insert(object)>0){
			return true;
		}
		return false;
	}
	
	/**
	 * 删除
	 * @return
	 */
	public boolean delete(String id){
		IStore store = StoreManager.create("templates_word");
		if(store.removeById(id)>0){
			return true;
		}
		return false;
	}
	
	/**
	 * 修改
	 * @param tw
	 * @return
	 */
	public boolean modify(TemplateWord tw){
		IStore store = StoreManager.create("templates_word");
		XObject object = new XObject("ID",tw.getId());
		object.put("WORD_NAME", tw.getWordName());
		object.put("WORD_PATH", tw.getWordPath());
		object.put("WORD_GENERATE_NAME", tw.getWordGeneratrName());
		object.put("WORD_VIRTUAL_PATH", tw.getWordVirtualPath());
		object.put("STORAGE_ADDRESS_ID", tw.getStorageAddressId());
		object.setUpdateUser();
		object.setUpdateTime();
		if(store.update(object)>0){
			return true;
		}
		return false;
	}
	
	private void copyDrToTemplateWord(DataRow row,TemplateWord tw){
		tw.setId(row.getString("ID"));
		tw.setWordName(row.getString("WORD_NAME"));
		tw.setWordPath(row.getString("WORD_PATH"));
		tw.setWordGeneratrName(row.getString("WORD_GENERATE_NAME"));
		tw.setWordVirtualPath(row.getString("WORD_VIRTUAL_PATH"));
		tw.setStorageAddressId(row.getString("STORAGE_ADDRESS_ID"));
	}
	/**
	 * 获取签章报告模板的第一条记录
	 * @return
	 * @throws Exception
	 */
	public TemplateWord getTemplateFilePath(){
		Sql sql  = new Sql("SELECT * FROM templates_word limit 1");
		DataTable dt = SqlSessionManager.getDataTable(sql);
		TemplateWord tw = new TemplateWord();
		if(dt !=null && dt.getRowCount() >0){
			DataRow row= dt.getRow(0);
			copyDrToTemplateWord(row,tw);
		}
		return tw;
	}
	
	/**
	 * 获取签章报告模板的第一条记录
	 * @return
	 * @throws Exception
	 */
	public TemplateWord getTemplateIdFilePath(String fielId){
		Sql sql  = new Sql("SELECT * FROM templates_word WHERE ID='"+fielId+"'");
		DataTable dt = SqlSessionManager.getDataTable(sql);
		TemplateWord tw = new TemplateWord();
		if(dt !=null && dt.getRowCount() >0){
			DataRow row= dt.getRow(0);
			copyDrToTemplateWord(row,tw);
		}
		return tw;
	}

	
}
